# -*- coding:utf-8 -*-
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation

fig = plt.figure()
ax311 = fig.add_subplot(3, 1, 1, xlim=(0, 2), ylim=(-4, 4))
ax312 = fig.add_subplot(3, 1, 2, xlim=(0, 2), ylim=(-4, 4))
ax313 = fig.add_subplot(3, 1, 3, xlim=(0, 2), ylim=(-4, 4))
line1, = ax311.plot([], [], lw=2, c='b')
line2, = ax312.plot([], [], lw=2, c='r')
line3, = ax313.plot([], [], lw=2, c='g')


def init():
    line1.set_data([], [])
    line2.set_data([], [])
    line3.set_data([], [])
    return line1, line2, line3


def animate(i):
    x = np.linspace(0, 2, 100)
    y1 = 2*np.cos(2*np.pi*(x))
    y2 = np.cos(4*np.pi*(x) + i*2*np.pi/50)
    y3 = y1 + y2
    line1.set_data(x, y1)
    line2.set_data(x, y2)
    line3.set_data(x, y3)
    return line1, line2, line3


anim = animation.FuncAnimation(fig, animate, init_func=init, frames=50, interval=50, blit=True)
plt.show()
